package cn.itcast.service.impl;

import cn.itcast.common.CustomException;
import cn.itcast.entity.Category;
import cn.itcast.entity.Dish;
import cn.itcast.entity.Setmeal;
import cn.itcast.mapper.CategoryMapper;
import cn.itcast.service.CategoryService;
import cn.itcast.service.DishService;
import cn.itcast.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category>
        implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    @Override
    public void removeDIY(long id) {
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(lambdaQueryWrapper);
        if (count>0){
            throw new CustomException("该分类和dish有关联不能删除");
        }

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(lambdaQueryWrapper1);
        if (count1 > 0){
            throw  new CustomException("该分类和setmeal有关不能删除");
        }

        super.removeById(id);
    }
}
